package com.example.service.impl;

import com.example.entity.Account;
import com.example.mapper.AccountMapper;
import com.example.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author: 21050134
 * @date: 2023/10/20 17:54
 * @description:
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
	@Resource
	private AccountMapper accountMapper;


	/**
	 * 扣减账户余额
	 *
	 * @param userId 用户id
	 * @param money  金额
	 */
	@Override
	public int decrease(Long userId, BigDecimal money) {
		log.info("------->account-service 开始查询账户余额");
		Account account = accountMapper.selectByUserId(userId);
		log.info("------->account-service 账户余额查询完成，" + account);
		if (account != null && account.getResidue().intValue() >= money.intValue()) {
			log.info("------->account-service 开始从账户余额中扣钱！");
			int decrease = accountMapper.decrease(userId, money);
			log.info("------->account-service 从账户余额中扣钱完成");
			return decrease;
		} else {
			log.info("账户余额不足，开始回滚！");
			throw new RuntimeException("账户余额不足！");
		}
	}
}
